package com.wondertek.service.mapper.business;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wondertek.service.dto.app.req.AppRoomSelectByUserIdReq;
import com.wondertek.service.dto.app.res.AppRoomSelectByUserIdRes;
import com.wondertek.service.dto.business.req.RoomPageReq;
import com.wondertek.service.dto.business.res.RoomPageRes;
import com.wondertek.service.entity.business.RoomPo;
import com.wondertek.service.entity.business.SitePo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @description:
 * @author: qinjianhui
 * @create: 2024-07-17 15:45
 **/
public interface RoomMapper extends BaseMapper<RoomPo> {

    /*
    * 根据设备sn码查询房间
    * */
    List<RoomPo> selectRoomByDeviceSn(@Param("snCode") String snCode);

    /*
    * 根据deviceId查已删除的room
    * */
    List<RoomPo> selectDeleteRoomByDeviceId(@Param("deviceId") String deviceId);

    /*
    * 恢复删除数据
    * */
    void updateRoomDelete(@Param("id") String id, @Param("roomName") String roomName,@Param("industryType") String industryType);

    /*
    * 根据站点查询房间
    * */
    List<RoomPo> selectRoomBySiteId(@Param("siteId") String siteId);

    /*
     * 用户查询房间数
     * */
    int selectRoomNumByUserId(@Param("userId") String userId);

    /*
     * 根据用户id查询房间
     * */
    List<AppRoomSelectByUserIdRes> selectByUserId(AppRoomSelectByUserIdReq req);


    /*
     * 根据deviceId和app用户id查询租赁分区
     * */
    @Select("select a.id,a.room_name,a.room_picture,a.device_id,b.device_name,a.mode from room a left join device b on b.id=a.device_id where a.is_delete=0 and b.id=#{deviceId} and a.id in( select b.room_id from rental_device b where b.consumer_id=#{consumerId} and b.is_delete=0 and b.end_date > now())")
    Page<AppRoomSelectByUserIdRes> findRoomByDcId(@Param("deviceId") String deviceId,@Param("consumerId") String consumerId,Page page);
    /*
     * 根据房间查询站点
     * */
    SitePo selectSiteByRoom(@Param("roomId") String roomId);

    /**
     * 查询网关设备-房间列表
     * @param req
     * @return
     */
    @Select("<script> select r.id roomId,r.room_name,r.room_code,i.dict_name industryName,r.mode,i.id industryId," +
            "(select count(1) from accessory a where a.is_delete = 0 and a.room_id = r.id) as accessorySum " +
            "from room r " +
            "left join sys_dict i on i.id=r.industry_type "+
            "where r.is_delete = 0 " +
            "<if test='req.deviceId != null and req.deviceId != \"\"'> " +
            "and r.device_id = #{req.deviceId} </if>  " +
            "<if test='req.roomName != null and req.roomName != \"\"'> " +
            "and r.room_name like concat('%',#{req.roomName},'%') </if>  " +
            "</script>")
    Page<RoomPageRes> roomPage(RoomPageReq req, Page page);
}

